package com.itic.market.mobile.uc.invite;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.qcode.QcodeUtils;
import com.itic.market.mobile.base.BaseMobileInterface;
import com.itic.market.system.user.persistence.model.MarketUser;


/**
 * 用户绑定手机号码接口类
 * @author 许小宝
 * 2017年07月26日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/uc/invite")
public class MarketUcInviteInterface
     extends BaseMobileInterface {
	
	/**
     * 用户手机号码发送验证码
     * @return 
     */
    @RequestMapping(value="/index")
    @ResponseBody
    public Map<String,Object> index( HttpServletRequest request, HttpServletResponse response, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    			    resultMap.put(DATA, user.getInvitingCode());
    				result = SUCCESS;
        			errorCode = SUCCESS_ERROR_CODE;
                	errorMsg = SUCCESS_ERROR_MSG; 
    			}else{
    				result = LOGIN;
        			errorCode = LOGIN_ERROR_CODE;
                	errorMsg = LOGIN_ERROR_MSG;  	
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
    /**
     * 用户手机号码发送验证码
     * @return 
     */
    @RequestMapping(value="/send")
    @ResponseBody
    public Map<String,Object> send( HttpServletRequest request, HttpServletResponse response, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				
    				Map<String, Object> dataMap = new HashMap<String,Object>();
    				if( StringHelper.isBlank( user.getInvitingImageUrl()) ){
    					//店铺地址
        				String URL = sysSettingService.getByKey("INVITE_SEND_URL_SIZE");
        				String[] split = URL.split(",");
        				String inviteUrl = split[0];
        				inviteUrl += user.getInvitingCode();
        				
        				String size = split[1];
        				String url =QcodeUtils.create(inviteUrl, Integer.parseInt(size), Integer.parseInt(size), "png");
        				user.setInvitingImageUrl(url);
        				marketUserService.edit(user);
    				}
    				
    				dataMap.put("url", user.getInvitingImageUrl() );
    				dataMap.put("name", user.getUserNickName());
    			    resultMap.put(DATA, dataMap);
    				result = SUCCESS;
        			errorCode = SUCCESS_ERROR_CODE;
                	errorMsg = SUCCESS_ERROR_MSG; 
    			}else{
    				result = LOGIN;
        			errorCode = LOGIN_ERROR_CODE;
                	errorMsg = LOGIN_ERROR_MSG;  	
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
}